<?php

namespace App\Http\Controllers\background;

use App\Http\Controllers\Controller;
use App\Models\Mysql\{XnMechantProfit,XnMerchant,XnOrder,XnTime};
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class MechantProfitController extends Controller
{
    public  function profitlist(Request $request)
    {
        $starttime = $request->post('starttime', '');
        $endtime = $request->post('endtime', '');
        $supplier_name = $request->post('mechant_name', '');
        $mechant_number = $request->post('mechant_number', '');
        $order_num = $request->post('order_num', '');
        $order_type = $request->post('order_type', '');
        $condition = [];
        if (!empty($starttime) && !empty($endtime)) {
            $condition[] = ['recharge_time', '>=', $starttime];
            $condition[] = ['recharge_time', '<=', $endtime];
        }
        if (!empty($supplier_name)) {
            $condition[] = ['mechant_name', 'like', '%' . $supplier_name . '%'];
        }
        if (!empty($mechant_number)) {
            $condition[] = ['merchant_number', '=', $mechant_number];

        }
        if (!empty($order_num)) {
            $condition[] = ['order_sn', '=', $order_num];
        }
        $condition[] = ['recharge_status', '=', 1];
        $condition[] = ['order_type', '=', $order_type];

        $list = XnOrder::where($condition)
            ->select(DB::raw('recharge_time,goods_belong,mechant_name,merchant_number,goods_name,order_sn,unitPrice,price,goods_num,all_money,ROUND(all_money-unitPrice*goods_num,4) as allprofit,children_supplier_status'))
            ->orderBy('recharge_time', 'desc')
            ->paginate(10)
            ->toArray();
        return successly($list);
    }

    public function monthprofitlist(Request $request){
        $year =$request->post('year','');
        $month = $request->post('month','');
        $order_type = $request->post('order_type','');
        $mechant_number = $request->post('mechant_number','');
        $condition[] = ['year','=',$year];
        if(!empty($mechant_number)){
            $condition[] = ['mechant_number','=',$mechant_number];
        }
        if($order_type == 1){
            $order_type = 2;
        }else if($order_type == 2){
            $order_type = 1;
        }
        $condition[] = ['order_type','=',$order_type];
        $select ='mechant_number,month,SUM(income_number) as i_number,SUM(income_money) as i_money,SUM(profit) as allProfit';
        if(!empty($month)){
            $condition[] = ['month','=',$month];
            $list = XnMechantProfit::where($condition)
                -> groupBy('mechant_number')
                -> select($select)
                -> paginate(10)
                -> toArray();
            if($list['data']){
                foreach($list['data'] as $k => $v){
                    $list['data'][$k]['mechant_name'] = XnMerchant::where('merchant_number','=',$v['mechant_number']) -> value('nickname');
                }
            }

        }else{
            $list = XnMechantProfit::where($condition)
                -> groupBy('mechant_number','month')
                -> selectRaw($select)
                -> paginate(10)
                -> toArray();
            if($list['data']){
                foreach($list['data'] as $k => $v){
                    $list['data'][$k]['mechant_name'] = XnMerchant::where('merchant_number','=',$v['mechant_number']) -> value('nickname');
                }
            }

        }
        return  successly($list);
    }

    public function monthprofitdetail(Request $request){
        $year = $request->post('year','');
        $month = $request->post('month','');
        $mechant_number =$request->post('mechant_number','');
        $condition[] = ['year','=',$year];
        $condition[] = ['month','=',$month];
        $condition[] = ['mechant_number','=',$mechant_number];
        $select = 'mechant_number,year,month,day,income_number,income_money,profit';
        $list = XnMechantProfit::where($condition)
            -> select(DB::raw($select))
            -> orderBy('create_time','desc')
            -> paginate(10)
            -> toArray();
        if($list['data']){
            foreach($list['data'] as $k => $v){
                $list['data'][$k]['mechant_name'] = XnMerchant::where('merchant_number','=',$v['mechant_number']) -> value('nickname');
            }
        }
        return successly($list);
    }

    public function dayprofitdetail(Request $request){
        $starttime = $request->post('starttime','');
        $endtime = $request->post('endtime','');
        $mechant_number = $request->post('mechant_number','');
        if (!empty($starttime) && !empty($endtime)) {
            $condition[] = ['recharge_time','>=',$starttime];
            $condition[] = ['recharge_time','<=',$endtime];
        }
        if(!empty($mechant_number)){
            $condition[] = ['merchant_number','=',$mechant_number];
        }


        $condition[] = ['recharge_status','=',1];
        $list = XnOrder::where($condition)
            -> select(DB::raw('recharge_time,goods_belong,mechant_name,goods_name,order_sn,unitPrice,price,goods_num,all_money,ROUND(all_money-unitPrice*goods_num,4) as allprofit,children_supplier_status'))
            -> orderBy('recharge_time','desc')
            -> paginate(10)
            -> toArray();
        return successly($list);
    }

    public function allprofit(Request $request){
        $year = $request->post('year','');
        $month = $request->post('month','');
        $day = $request->post('day','');
        $mechant_number =$request->post('mechant_number','');
        $order_type = $request->post('order_type','');
        if($order_type == 1){
            $order_type = 2;
        }else if($order_type == 2){
            $order_type = 1;
        }
        if(!empty($order_type)){
            $profit_condition[] = ['order_type',$order_type];
        }

        $profit_condition[] = ['year','=',$year];
        if(!empty($mechant_number)){
            $profit_condition[] = ['mechant_number',$mechant_number];
        }
        if(!empty($month)){
            $profit_condition[] = ['month','=',$month];
        }
        if(!empty($day)){
            $profit_condition[] = ['day','=',$day];
        }
        $moneys = XnMechantProfit:: where($profit_condition) -> sum('profit');
        return successly(['moneys'=>$moneys]);
    }

    public function timeprofit(Request $request){
        $starttime = $request->post('starttime','');
        $endtime = $request->post('endtime','');
        $supplier_name = $request->post('mechant_name','');
        $mechant_number = $request->post('mechant_number','');
        $order_num = $request->post('order_num','');
        $order_type = $request->post('order_type','');
        $condition = [];
        if (!empty($starttime) && !empty($endtime)) {
            $condition[] = ['recharge_time','>=',$starttime];
            $condition[] = ['recharge_time','<=',$endtime];
        }
        if(!empty($supplier_name)){
            $condition[] = ['mechant_name','like','%'.$supplier_name.'%'];
        }
        if(!empty($mechant_number)){
            $condition[] = ['merchant_number','=',$mechant_number];
        }
        if(!empty($order_num)){
            $condition[] = ['order_sn','=',$order_num];
        }
        $condition[] =['recharge_status','=',1];
        $condition[] = ['order_type','=',$order_type];
        $moneyinfo = XnOrder::where($condition)
            -> select(DB::raw('ROUND(SUM(all_money-unitPrice*goods_num),4) as allprofit'))
            -> first()
            -> toArray();
        if(!$moneyinfo['allprofit']){
            $moneys =  0;
        }else{
            $moneys = $moneyinfo['allprofit'];
        }
        return successly(['moneys'=>$moneys]);
    }

    public function years(Request $request){
        $type = $request -> post('type');
        $years = XnTime::groupBy('year')
            ->where('type','=',$type)
            -> select('year')
            -> orderBy('year' ,'desc')
            -> get()
            -> toArray();
        return successly($years);
    }

    public function months(Request $request){
        $year = $request->post('year','');
        $type = $request -> post('type');
        $condition[] = ['year','=',$year];
        $condition[] = ['type','=',$type];
        $res = XnTime ::where($condition) -> pluck('month');
        return successly($res);
    }
}
